package Hot100;

import Tree.TreeNode;

public class 二叉树的直径 {

    private int diameter = 0;

    public int diameterOfBinaryTree(TreeNode root) {
        depthOfBinaryTree(root);
        return diameter;
    }

    private int depthOfBinaryTree(TreeNode root) {
        if (root == null) return 0;
        int left = depthOfBinaryTree(root.left);
        int right = depthOfBinaryTree(root.right);
        diameter = Math.max(diameter, left + right);
        return Math.max(left, right) + 1;
    }
}
